package com.stock_t.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.stock_t.entity.StockPendingBuyPool;
import com.stock_t.entity.StockPool;
import com.stock_t.page.TableDataInfo;
import com.stock_t.service.StockPendingBuyPoolService;
import com.stock_t.utils.PageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 待买入股票池控制器
 */
@Slf4j
@RestController
@RequestMapping("/stock/pending/buy/pool")
public class StockPendingBuyPoolController {

    @Autowired
    private StockPendingBuyPoolService stockPendingBuyPoolService;

    /**
     * 获取待买入股票池列表
     */
    @PostMapping("/list")
    public Map<String, Object> list(@RequestBody StockPendingBuyPool stockPendingBuyPool) {
        LambdaQueryWrapper<StockPendingBuyPool> queryWrapper = new LambdaQueryWrapper<>(stockPendingBuyPool);
        queryWrapper.orderByDesc(StockPendingBuyPool::getBuyPrice);
        List<StockPendingBuyPool> list = stockPendingBuyPoolService.list(queryWrapper);
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("data", list);
        result.put("msg", "success");
        return result;
    }
    
    /**
     * 分页查询待买入股票池
     */
    @PostMapping("/page")
    public TableDataInfo page(@RequestBody StockPendingBuyPool stockPendingBuyPool) {
        // 使用PageUtils开启分页
        Page<StockPendingBuyPool> page = PageUtils.startPage();
        log.info("分页查询待买入股票池，查询条件：{}", stockPendingBuyPool);
        // 构建查询条件
        QueryWrapper<StockPendingBuyPool> queryWrapper = new QueryWrapper<>(stockPendingBuyPool);
        stockPendingBuyPoolService.page(page, queryWrapper);
        
        return PageUtils.getDataTable(page);
    }
    
    /**
     * 根据ID获取待买入股票池
     */
    @GetMapping("/{id}")
    public Map<String, Object> getById(@PathVariable("id") Long id) {
        StockPendingBuyPool stockPendingBuyPool = stockPendingBuyPoolService.getById(id);
        
        Map<String, Object> result = new HashMap<>();
        if (stockPendingBuyPool != null) {
            result.put("code", 200);
            result.put("data", stockPendingBuyPool);
            result.put("msg", "success");
        } else {
            result.put("code", 404);
            result.put("msg", "待买入股票池不存在");
        }
        
        return result;
    }
    
    /**
     * 保存待买入股票池（新增或更新）
     */
    @PostMapping("/save")
    public Map<String, Object> save(@RequestBody StockPendingBuyPool stockPendingBuyPool) {
        boolean success = stockPendingBuyPoolService.saveOrUpdate(stockPendingBuyPool);
        
        Map<String, Object> result = new HashMap<>();
        if (success) {
            result.put("code", 200);
            result.put("msg", "保存成功");
        } else {
            result.put("code", 500);
            result.put("msg", "保存失败");
        }
        
        return result;
    }
    
    /**
     * 更新待买入股票池
     */
    @PutMapping("/update")
    public Map<String, Object> update(@RequestBody StockPendingBuyPool stockPendingBuyPool) {
        // 确保记录存在
        StockPendingBuyPool existStockPendingBuyPool = stockPendingBuyPoolService.getById(stockPendingBuyPool.getId());
        
        Map<String, Object> result = new HashMap<>();
        if (existStockPendingBuyPool == null) {
            result.put("code", 404);
            result.put("msg", "待买入股票池不存在");
            return result;
        }
        
        boolean success = stockPendingBuyPoolService.updateById(stockPendingBuyPool);
        
        if (success) {
            result.put("code", 200);
            result.put("msg", "更新成功");
        } else {
            result.put("code", 500);
            result.put("msg", "更新失败");
        }
        
        return result;
    }
    
    /**
     * 删除待买入股票池
     */
    @DeleteMapping("/{id}")
    public Map<String, Object> delete(@PathVariable("id") Long id) {
        // 确保记录存在
        StockPendingBuyPool existStockPendingBuyPool = stockPendingBuyPoolService.getById(id);
        
        Map<String, Object> result = new HashMap<>();
        if (existStockPendingBuyPool == null) {
            result.put("code", 404);
            result.put("msg", "待买入股票池不存在");
            return result;
        }
        
        boolean success = stockPendingBuyPoolService.removeById(id);
        
        if (success) {
            result.put("code", 200);
            result.put("msg", "删除成功");
        } else {
            result.put("code", 500);
            result.put("msg", "删除失败");
        }
        
        return result;
    }
    
    /**
     * 批量删除待买入股票池
     */
    @DeleteMapping("/batch")
    public Map<String, Object> batchDelete(@RequestBody List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            Map<String, Object> result = new HashMap<>();
            result.put("code", 400);
            result.put("msg", "请选择要删除的记录");
            return result;
        }
        
        boolean success = stockPendingBuyPoolService.removeByIds(ids);
        
        Map<String, Object> result = new HashMap<>();
        if (success) {
            result.put("code", 200);
            result.put("msg", "批量删除成功");
        } else {
            result.put("code", 500);
            result.put("msg", "批量删除失败");
        }
        
        return result;
    }
}